package com.bw.imageloader.impl;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bw.imageloader.IImageLoader;
import com.bw.imageloader.ImageOptions;

import java.io.File;

/**
 * @author:created by hahajing
 * 时间：2024/4/26 0026
 * 企鹅号:444511958
 * 描述:
 */
public class GlideStrategy implements IImageLoader {
    @Override
    public void loadFileIntoImageView(Context context, String url, ImageView imageView, ImageOptions options) {
        loadImageView(context,url,imageView,options);
    }

    @Override
    public void loadFileIntoImageView(Context context, byte[] content, ImageView imageView, ImageOptions options) {
        loadImageView(context,content,imageView,options);
    }

    @Override
    public void loadFileIntoImageView(Context context, int resId, ImageView imageView, ImageOptions options) {
        loadImageView(context,resId,imageView,options);
    }

    @Override
    public void loadFileIntoImageView(Context context, File file, ImageView imageView, ImageOptions options) {
        loadImageView(context,file,imageView,options);
    }

    private void loadImageView(Context context, Object content, ImageView imageView, ImageOptions options) {
        RequestManager requestManager = Glide.with(context);
        RequestOptions requestOptions=new RequestOptions();
        if (options!=null){
            if (options.placeImage>0){
                requestOptions.placeholder(options.placeImage);
            }
            if (options.errorImage>0){
                requestOptions.error(options.errorImage);
            }
            if (options.skipMemoryCache){
                requestOptions.skipMemoryCache(true);
            }
            if (options.skipDiskCache){
                requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
            }
        }
//        requestManager.setDefaultRequestOptions(requestOptions);
        requestManager.load(content)
                .apply(requestOptions)
                .into(imageView);

    }


}
